﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace System8Equations
{
    /// <summary>
    /// Attempt to solve System 8 given the inputs.
    /// </summary>
    public class Solver
    {
        private double sqr(double r)
        {
            return r * r;
        }

        /// <summary>
        /// Just make life simple...
        /// </summary>
        public struct System8Parameters
        {
            public double n, p;
            public double n1, n2;
            public double p1, p2;
            public double n12, p12;
        }

        public struct System8Alphas
        {
            public double alpha_1, alpha_2, alpha_3, alpha_4, alpha_5, alpha_6;
        }

        public struct System8Results
        {
            public double eptag1, eptag2;
            public double epmistag1, epmistag2;
        }

        public System8Results CalculateSystem8(System8Parameters param, System8Alphas alphas)
        {
            return CalculateSystem8(param.n, param.p,
                param.n1, param.n2,
                param.p1, param.p2,
                param.n12, param.p12,
                alphas.alpha_1, alphas.alpha_2, alphas.alpha_3,
                alphas.alpha_4, alphas.alpha_5, alphas.alpha_6);
        }

        public System8Results CalculateSystem8(
            double n, double p,
            double n1, double n2,
            double p1, double p2,
            double n12, double p12,
            double alpha_1,
            double alpha_2,
            double alpha_3,
            double alpha_4,
            double alpha_5,
            double alpha_6)
        {
            double nb = -100000000;
            double pb = -100000000;
            double nl = -100000000;
            double pl = -100000000;

            double eptag1 = -100000000;
            double epmistag1 = -100000000;
            double eptag2 = -100000000;
            double epmistag2 = -100000000;

            /////////////////////////// Solution 1


            if (Math.Pow((n12 * p - n2 * p1 - n * p12 + n1 * p2), 2) + 4 * (n1 * p - n * p1) * (n2 * p12 - n12 * p2) != 0)
            {
                nl = ((-n) * n12 * p + 2 * n1 * n2 * p - n * n2 * p1 + Math.Pow(n, 2) * p12 - n * n1 * p2 + n * Math.Sqrt(Math.Pow((n12 * p - n2 * p1 - n * p12 + n1 * p2), 2) + 4.0 * (n1 * p - n * p1) * (n2 * p12 - n12 * p2))) / (2.0 * Math.Sqrt(Math.Pow((n12 * p - n2 * p1 - n * p12 + n1 * p2), 2) + 4.0 * (n1 * p - n * p1) * (n2 * p12 - n12 * p2)));
                nb = (n * n12 * p - 2 * n1 * n2 * p + n * n2 * p1 - Math.Pow(n, 2) * p12 + n * n1 * p2 + n * Math.Sqrt(Math.Pow((n12 * p - n2 * p1 - n * p12 + n1 * p2), 2) + 4.0 * (n1 * p - n * p1) * (n2 * p12 - n12 * p2))) / (2.0 * Math.Sqrt(Math.Pow((n12 * p - n2 * p1 - n * p12 + n1 * p2), 2) + 4.0 * (n1 * p - n * p1) * (n2 * p12 - n12 * p2)));
                pl = ((-n12) * Math.Pow(p, 2) + n2 * p * p1 + n * p * p12 + n1 * p * p2 - 2.0 * n * p1 * p2 + p * Math.Sqrt(Math.Pow((n12 * p - n2 * p1 - n * p12 + n1 * p2), 2) + 4.0 * (n1 * p - n * p1) * (n2 * p12 - n12 * p2))) / (2.0 * Math.Sqrt(Math.Pow((n12 * p - n2 * p1 - n * p12 + n1 * p2), 2) + 4.0 * (n1 * p - n * p1) * (n2 * p12 - n12 * p2)));
                pb = (n12 * Math.Pow(p, 2) - n2 * p * p1 - n * p * p12 - n1 * p * p2 + 2.0 * n * p1 * p2 + p * Math.Sqrt(Math.Pow((n12 * p - n2 * p1 - n * p12 + n1 * p2), 2) + 4.0 * (n1 * p - n * p1) * (n2 * p12 - n12 * p2))) / (2.0 * Math.Sqrt(Math.Pow((n12 * p - n2 * p1 - n * p12 + n1 * p2), 2) + 4.0 * (n1 * p - n * p1) * (n2 * p12 - n12 * p2)));
            }

            if ((2 * n2 * p - 2 * n * p2) != 0)
            {
                epmistag1 = (n12 * p + n2 * p1 - n * p12 - n1 * p2 + Math.Sqrt(Math.Pow((n12 * p - n2 * p1 - n * p12 + n1 * p2), 2) + 4.0 * (n1 * p - n * p1) * (n2 * p12 - n12 * p2))) / (2.0 * n2 * p - 2.0 * n * p2);
                //double eptag1_c = -(((-n12)*p - n2*p1 + n*p12 + n1*p2 + Math.Sqrt(Math.Pow((n12*p - n2*p1 - n*p12 + n1*p2),2) + 4.*(n1*p - n*p1)*(n2*p12 - n12*p2)))/(2.*n2*p - 2.*n*p2));
                eptag1 = (-(n12 * p) + n1 * p2 * alpha_3 - n2 * p1 * alpha_6 + n * p12 * alpha_3 * alpha_6 + Math.Sqrt(-4 * (-(n12 * p1) + n1 * p12 * alpha_3) * (-(n2 * p) + n * p2 * alpha_3) * alpha_6 + sqr(n12 * p - n1 * p2 * alpha_3 + n2 * p1 * alpha_6 - n * p12 * alpha_3 * alpha_6))) / (2.0 * (-(n2 * p) + n * p2 * alpha_3) * alpha_6);
            }

            if ((2 * n1 * p - 2 * n * p1) != 0)
            {
                epmistag2 = (n12 * p - n2 * p1 - n * p12 + n1 * p2 + Math.Sqrt(Math.Pow((n12 * p - n2 * p1 - n * p12 + n1 * p2), 2) + 4.0 * (n1 * p - n * p1) * (n2 * p12 - n12 * p2))) / (2.0 * n1 * p - 2.0 * n * p1);
                eptag2 = -(((-n12) * p + n2 * p1 + n * p12 - n1 * p2 + Math.Sqrt(Math.Pow((n12 * p - n2 * p1 - n * p12 + n1 * p2), 2) + 4.0 * (n1 * p - n * p1) * (n2 * p12 - n12 * p2))) / (2.0 * n1 * p - 2.0 * n * p1));
            }

            return new System8Results()
            {
                epmistag1 = epmistag1,
                epmistag2 = epmistag2,
                eptag1 = eptag1,
                eptag2 = eptag2
            };
        }
    }
}
